package com.example.springboot_mvc_demo.controller;

import cn.hutool.http.server.HttpServerResponse;
import com.example.springboot_mvc_demo.dto.ClazzRoom;
import com.example.springboot_mvc_demo.dto.User;
import com.example.springboot_mvc_demo.utils.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/7/15:15:08
 */

@SuppressWarnings({"unchecked", ""})
@RestController
@Validated// 针对当前类里面有@notbank这样的注解
@RequestMapping("/form")
public class FormController {
    private static final Logger log = LoggerFactory.getLogger(FormController.class);

    /**
     *  接收字符串传参！
     *  required = false 表示当前参数不是必传
     * @param name
     */
    @RequestMapping("/StringValue")
    @ResponseBody
    public String receiveParameters(@RequestParam(value = "username",required = false) String name){
        System.out.println(name);
        return name;
    }


    /**
     *接收前端form提交的数组
     * @param ids
     */
    @RequestMapping("/Arrays")
    @ResponseBody
    public Map receiveParameters(int[] ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * 传入一个数组，后端自动封装成List集合
     * @param ids
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map receiveParameters(@RequestParam List<Integer> ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * 传入一堆数据，后端自动封装成Map集合
     * 接收文件  MultipartFile file
     * @param user
     */
    @ResponseBody
    @RequestMapping("/map")
    public Map receiveParameters(@RequestParam Map<String,Object> user,
                                 MultipartFile file){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }
    /**
     *  传入一堆数据，后端自动封装成User对象
     * @param user
     */
    @GetMapping("/user")
    public Map receiveParameters(User user){
        log.info("{}",user);
       Map<Object, Object> hashMap = new HashMap<>();
       hashMap.put("data",user);
       hashMap.put("code",200);
       return hashMap;
   }

    /**
     *  传入一堆数据，后端自动封装成User对象
     * @param user
     */
    @PostMapping("/user2Json")
    public Map receiveParameters2Json(@RequestBody User user){
        log.info("{}",user);
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 复合对象数据传输
     * @param clazzRoom
     */
    @GetMapping("/ClazzUserValue")
    public Map receiveParameters(ClazzRoom clazzRoom){
        log.info("{}",clazzRoom);
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",clazzRoom);
        hashMap.put("code",200);
        return hashMap;
    }


    /**
     * 嵌套对象 使用json传输
     * @param clazzRoom
     */
    @PostMapping("/ClazzUserValue2Json")
    public Map receiveParameters2Json(@RequestBody ClazzRoom clazzRoom){
        log.info("{}",clazzRoom);
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",clazzRoom);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 使用表单或者query 传时间参数
     */
    @GetMapping("/dataFormat2Form")
    public Map dataFormat2Form(User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("user",user);
        hashMap.put("code",200);
        return hashMap;
    }

//
//    findByIdAndName/1/xm
    @GetMapping("findByIdAndName/{userId}/{username}")
    public Map findByIdAndName(@PathVariable("userId") String userId, @PathVariable("username")String username,
                               HttpServletResponse response){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("id",userId);
        hashMap.put("name",username);
        //response.setStatus(500);
        return hashMap;
    }

    /**
     * 参数校验
     * @param user
     */
    @GetMapping("/checkParam")
    public Map checkParam(@Validated User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("data",user);
        hashMap.put("code",200);
        return hashMap;
    }
    /**
     * 参数校验
     * @param name
     */
    @GetMapping("/findStudentById")
    public AjaxResult findStudentById(@NotBlank(message = "name 不能为空") String name){
        User user = new User();
        user.setAge(10);
        user.setName("123");
        //AjaxResult.success("用户登录成功");
        //AjaxResult ajaxResult = AjaxResult.error("密码不正常");
        return AjaxResult.success(user);
    }

}
